/**
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 *
 * @data:2017年10月17日
 * @author:MaShibo
 */
package com.wttech.dzzf.process.dao;

import java.util.List;

import javax.annotation.Resource;

import cn.com.taiji.sdk.model.comm.protocol.transaction.RechargeUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.transaction.ReversalUploadRequest;
import com.wtkj.vo.dzzf.base.kfwd.TransIn;
import com.wtkj.vo.dzzf.base.kfwd.WriteOffTransIn;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.dao.impl.ListResultSetExtractor;
import com.wtkj.vo.dzzf.base.kfwd.CardInfoOpt;
import org.springframework.util.CollectionUtils;

/**
 * 冲正信息DAO
 *
 * @author MaShibo
 * @date 2017年10月17日
 */
@Repository
public class ReverSalInfoDao extends BaseDataTemplate {


    @Resource(name = "dzzfJdbcTemplate")
    private JdbcTemplate dzzfJdbcTemplate;

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate testJdbcTemplate;

    @SuppressWarnings("unchecked")
    public List<CardInfoOpt> queryReverSalInfos() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT A.* ");
        sql.append("   FROM AC_CARDINFOOPT A ");
        sql.append("  WHERE A.OPTTYPE = '1302' ");
        sql.append("    AND A.STATE = 1 ");
        sql.append("    AND A.OPTON >= TO_DATE('20170101', 'yyyyMMdd') ");
        sql.append("    AND A.OPTON < TO_DATE('20170901', 'yyyyMMdd') ");
        return (List<CardInfoOpt>) dzzfJdbcTemplate.query(sql.toString(),
                new ListResultSetExtractor(new CardInfoOpt()));
    }


    @SuppressWarnings("unchecked")
    public List<WriteOffTransIn> queryWriteOffTransinInfos() {
        final String sql =
                "SELECT A.* " +
                        "  FROM TR_WRITEOFFTRANSIN A " +
                        "  JOIN TR_TRANSIN TR " +
                        "    ON A.TRADENOINC = TR.TRADENO " +
                        " WHERE A.STATE = 1" +
                        " AND A.OPTON < TO_DATE('20171117', 'YYYYMMDD') ";

        return (List<WriteOffTransIn>) dzzfJdbcTemplate.query(sql, new ListResultSetExtractor(new WriteOffTransIn()));
    }

    @SuppressWarnings("unchecked")
    public TransIn queryTransinInfoByTradeNo(String tradeNo) {
        List<TransIn> transIns = (List<TransIn>) dzzfJdbcTemplate.query("SELECT * FROM TR_TRANSIN TR WHERE TR.TRADENO=? ", new ListResultSetExtractor(new TransIn()), tradeNo);
        return CollectionUtils.isEmpty(transIns) ? null : transIns.get(0);
    }

    public List<ReversalUploadRequest> queryReversalUploadInfos(){
        final String sql = "SELECT * FROM BS_REVERSAL A ";
        return testJdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(ReversalUploadRequest.class));
    }

    public RechargeUploadRequest queryRechargeUploadInfo(String rechargeId){
        final String sql = "SELECT * FROM BS_RECHARGE A WHERE A.ID=? ";
        List<RechargeUploadRequest> rechargeUploadRequests = testJdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(RechargeUploadRequest.class), rechargeId);
        return CollectionUtils.isEmpty(rechargeUploadRequests) ? null : rechargeUploadRequests.get(0);
    }

}
